﻿using MySql.Data.MySqlClient;
using System;
using System.Data;

namespace WindowsFormsApp1
{
    class MysqlHelper
    {
        public static string connStr = "Database=db3;Data Source=127.0.0.1;User Id=root;Password=1770279454@a;pooling=false;CharSet=utf8;port=3306";
        //public static string connStr = "Server=127.0.0.1;Database=db3;Uid=root;Pwd=1770279454@a;Pooling=false;CharSet=utf8";

        public static bool ExeSql(String sqlStr, params MySqlParameter[] para)
        {
            using(MySqlConnection conn=new MySqlConnection(connStr))
            {
 
                using (MySqlCommand command = new MySqlCommand(sqlStr,conn))
                {
                    try
                    {
                        conn.Open();
                        Console.WriteLine("建立连接了");
                        if (para != null)
                        {
                            foreach (MySqlParameter p in para)
                            {
                                command.Parameters.AddWithValue(p.ParameterName, p.Value);
                            }
                        }
                        return command.ExecuteNonQuery() > 0;

                    }
                    catch (Exception e)
                    {
                        return false;
                    }
                }
            }
        }

        public static DataSet ExeSqlQuery(String sqlStr, params MySqlParameter[] para)
        {
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                using (MySqlCommand command = new MySqlCommand(sqlStr, conn))
                {
                    DataSet ds=new DataSet();
                    try
                    {
                        if (para != null)
                        {
                            foreach (MySqlParameter p in para)
                            {
                                command.Parameters.AddWithValue(p.ParameterName, p.Value);
                            }
                        }
                        MySqlDataAdapter da = new MySqlDataAdapter(command);
                        da.Fill(ds);
                        return ds;
                    }
                    catch(Exception e)
                    {
                        return null;
                    }

                  
                }
            }
        }


    }
}
